package org.log4jeditor.xml.editor.hyperlink;

/*******************************************************************************
 * Copyright (c) Standards for Technology in Automotive Retail and others
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *     
 * Modified version of 
 * http://dev.eclipse.org/viewcvs/index.cgi/incubator/sourceediting/tests/org.eclipse.wst.xsl.ui.tests/src/org/eclipse/wst/xsl/ui/tests/hyperlinkdetector/TestXSLHyperlinkDetector.java?root=WebTools_Project&view=markup
 *******************************************************************************/

import junit.framework.Assert;

import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.junit.Before;
import org.junit.Test;
import org.log4jconfig.xml.editor.hyperlink.AppenderRefValueHyperlinkDetector;
import org.log4jeditor.xml.editor.AbstractUITestCase;

/**
 * Tests everything about code completion and code assistance.
 * 
 */
public class AppenderRefHyperlinkDetectorTestCase extends AbstractUITestCase {

	@Before
	public void setup() throws Exception {
		setupEditorConfiguration("log4j-hyperlink.xml");
	}

	/**
	 * Get the content completion proposals at <code>lineNumber</code>,
	 * <code>columnNumber</code>.
	 * 
	 * @param lineNumber
	 * @param columnNumber
	 * @return
	 * @throws Exception
	 */
	private IHyperlink[] getHyperlinks(int offset) throws Exception {
		IRegion region = new Region(offset, 0);
		return new AppenderRefValueHyperlinkDetector().detectHyperlinks(sourceViewer, region, true);
	}

	@Test
	public void testDetectHyperlinksOnExistingRootAppenderRef() throws Exception {
		int offset = getDocumentOffset(56, 30, "appender-ref");
		IHyperlink[] links = getHyperlinks(offset);
		Assert.assertNotNull("No links returned", links);
		Assert.assertEquals("Unexpected number of links", 1, links.length);
	}

	@Test
	public void testDetectHyperlinksOnExistingLoggerAppenderRef() throws Exception {
		int offset = getDocumentOffset(31, 31, "appender-ref");
		IHyperlink[] links = getHyperlinks(offset);
		Assert.assertNotNull("No links returned", links);
		Assert.assertEquals("Unexpected number of links", 1, links.length);
		Assert.assertNotNull("No region for hyperlink", links[0].getHyperlinkRegion());
		Assert.assertNotNull("No text for hyperlink", links[0].getHyperlinkText());
		Assert.assertNotNull("No type label for hyperlink", links[0].getTypeLabel());
		links[0].open();
	}

	@Test
	public void testDetectHyperlinksOnUnexistingLoggerAppenderRef() throws Exception {
		int offset = getDocumentOffset(27, 31, "appender-ref");
		IHyperlink[] links = getHyperlinks(offset);
		Assert.assertNull("Unexpected links returned", links);
	}

	@Test
	public void testDetectHyperlinksOnUnexpectedRegion1() throws Exception {
		int offset = getDocumentOffset(16, 26, "appender");
		IHyperlink[] links = getHyperlinks(offset);
		Assert.assertNull("Unexpected links returned", links);
	}

	@Test
	public void testDetectHyperlinksOnUnexpectedRegion2() throws Exception {
		int offset = getDocumentOffset(29, 22, "logger");
		IHyperlink[] links = getHyperlinks(offset);
		Assert.assertNull("Unexpected links returned", links);
	}

	@Test
	public void testDetectHyperlinksOnUnexpectedRegion3() throws Exception {
		int offset = getDocumentOffset(29, 15, "logger");
		IHyperlink[] links = getHyperlinks(offset);
		Assert.assertNull("Unexpected links returned", links);
	}

}